<template>
  <div class="crash-report4">
    <el-form :inline="true">
      <el-form-item label="时间">
        <el-date-picker
          size="small"
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format='yyyy-MM-dd HH:mm:ss'
          :default-time="['00:00:00', '23:59:59']">
          </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button size="small" :loading="false" @click="getPageData">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <div id="os-report" style="width: 100%; height:380px;" v-loading="osLoading"></div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import echarts from 'echarts'
  import moment from 'moment'
  import lodash from 'lodash'

  var yAndriodData = ['7.0.3', '6.0.2', '6.1.1', '6.0.0', '7.1.2', '7.0.0', '7.1.1', '6.0.8', '6.0.4', '8.0.0']
  var yIosData = ['11.2.1', '11.0.1', '10.2.1', '10.0.2', '10.1.1', '10.2.0', '9.3.3', '9.3.5', '8.1.1', '8.0.2']
  var andriodData = [0.023, 0.026, 0.028, 0.031, 0.035, 0.039, 0.044, 0.050, 0.074, 0.099]
  var iosData = [0.035, 0.038, 0.039, 0.041, 0.045, 0.049, 0.050, 0.054, 0.071, 0.132]

  export default {
    data () {
      return {
        dateRange: [moment().format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD HH:mm:ss')],
        osLoading: false,
        chartsInstance: {
          os: ''
        },
        yAndriodData: yAndriodData,
        yIosData: yIosData,
        andriodData: andriodData,
        iosData: iosData
      }
    },
    activated () {
      this.getPageData()
      // this.initEcharts()
    },
    methods: {
      getPageData () {
        var vm = this
        var params = []
        var url = '/crash/getVersionRate'

        vm.osLoading = true

        if (vm.dateRange !== null && vm.dateRange !== '') {
          var startTime = vm.dateRange[0]
          params.push('startTime=' + moment(startTime).format('YYYY-MM-DD HH:mm:ss'))

          var endTime = vm.dateRange[1]
          params.push('endTime=' + moment(endTime).format('YYYY-MM-DD HH:mm:ss'))
        }

        url += (params.length > 0 ? '?' + params.join('&') : '')
        // console.log(url)

        vm.$http({
          url: vm.$http.adornUrl(url),
          methods: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            vm.yAndriodData = []
            vm.andriodData = []
            vm.yIosData = []
            vm.iosData = []
            if (data.data.Android) {
              var androidResult = lodash.sortBy(data.data.Android, function (item) {
                return -item.value
              })
              lodash.take(androidResult, 10).reverse().map(item => {
                vm.yAndriodData.push(item.version)
                vm.andriodData.push(item.value.toPrecision(2))
              })
            }
            if (data.data.iOS) {
              var iosResult = lodash.sortBy(data.data.iOS, function (item) {
                return -item.value
              })
              lodash.take(iosResult, 10).reverse().map(item => {
                vm.yIosData.push(item.version)
                vm.iosData.push(item.value.toPrecision(2))
              })
            }
            this.initEcharts()
          }
        })
      },
      initEcharts () {
        var vm = this
        vm.chartsInstance.os = echarts.init(document.getElementById('os-report'))
        vm.getOsReport()
        window.addEventListener('resize', () => {
          vm.chartsInstance.os.resize()
        })
      },
      getOsReport () {
        var vm = this
        let isShow = vm.andriodData.length === 0 && vm.iosData.length === 0
        var option = {
          title: [{
            text: '主流操作系统Top 10应用崩溃率高系统版本'
          }, {
            show: isShow,
            text: '暂无数据',
            top: '40%',
            left: 'center',
            textStyle: {
              fontSize: 13
            }
          }],
          tooltip: {
            // trigger: 'axis',
            // formatter: '{b} <br> 合格率: {c}%'
            formatter: function (params) {
              return params.marker + ' ' + params.name + ': ' + params.value
            }
          },
          legend: {
            top: 40,
            data: ['Android系统 Top 10 应用崩溃率高系统版本', 'IOS Top 10 应用崩溃率高系统版本']
          },
          // color: ['#69AD4A', '#0C81C6'],
          color: ['#4793C0', '#00A65A'],
          grid: [{
            left: '4%',
            width: '43%',
            top: '20%',
            right: '10%'
          }, {
            left: '56%',
            width: '45%',
            top: '20%',
            right: '10%'
          }],
          xAxis: [{
            gridIndex: 0,
            type: 'value',
            // boundaryGap: [0, 0.01],
            // min: 0,
            // max: 0.14,
            axisLabel: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            splitLine: {
              show: false
            }
          }, {
            gridIndex: 1,
            type: 'value',
            // boundaryGap: [0, 0.01],
            // min: 0,
            // max: 0.14,
            axisLabel: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            splitLine: {
              show: false
            }
          }],
          yAxis: [{
            type: 'category',
            gridIndex: 0,
            data: vm.yAndriodData,
            axisLabel: {
              margin: 10,
              inside: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            }
          }, {
            type: 'category',
            gridIndex: 1,
            data: vm.yIosData,
            axisLabel: {
              margin: 10,
              inside: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            }
          }],
          series: [{
            type: 'bar',
            xAxisIndex: 0,
            yAxisIndex: 0,
            name: 'Android系统 Top 10 应用崩溃率高系统版本',
            label: {
              normal: {
                show: true,
                position: 'right',
                formatter: '{c}%'
                // color: '#fff'
              }
            },
            data: vm.andriodData
          }, {
            type: 'bar',
            xAxisIndex: 1,
            yAxisIndex: 1,
            name: 'IOS Top 10 应用崩溃率高系统版本',
            label: {
              normal: {
                show: true,
                position: 'right',
                formatter: '{c}%'
                // color: '#fff'
              }
            },
            data: vm.iosData
          }]
        }
        vm.chartsInstance.os.setOption(option)
        vm.osLoading = false
      }
    }
  }
</script>
<style scoped>
.el-form{
  text-align: right;
  margin-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.el-button{
  padding: 8px 15px;
  font-size: 14px;
}
</style>

